#include"../mqserver/mq_bingding.hpp"
#include<gtest/gtest.h>
bitmq::BingdingManager::ptr bdmep;

class Binding_Test:public testing::Environment{
    public:
    virtual void SetUp()override
    {
        bdmep=std::make_shared<bitmq::BingdingManager>("./data/meta.db");
        std::cout<<"数据的初始化完成！"<<std::endl;
    }
    virtual void TearDown()override
    {
       bdmep->clear();
        std::cout<<"数据的清理完成！"<<std::endl;
    }
};

TEST(bingding_test,binding_test)
{
    bdmep->bind("exchange1","queue1","news.music#",true);
    bdmep->bind("exchange1","queue2","news.music.pop",true);
    bdmep->bind("exchange1","queue3","news.music.sport",true);
    bdmep->bind("exchange2","queue1","news.music#",true);
    bdmep->bind("exchange2","queue2","news.music.rap",true);
    bdmep->bind("exchange2","queue3","news.music.gossip",true);

    ASSERT_EQ(bdmep->size(),6);
    ASSERT_EQ(bdmep->exists("exchange1","queue1"),true);
}
TEST(bingding_test,select_test)
{
    bitmq::QueueBingdingMap qbmap=bdmep->getExchangeBingdings("exchange1");
    ASSERT_NE(qbmap.find("queue1"),qbmap.end());
    ASSERT_NE(qbmap.find("queue2"),qbmap.end());
    ASSERT_NE(qbmap.find("queue3"),qbmap.end());

    bitmq::Bingding::ptr bdptr=bdmep->getQueueBingdings("exchange1","queue1");
    ASSERT_EQ(bdptr->_exchange_name,"exchange1");
    ASSERT_EQ(bdptr->_queue_name,"queue1");
    ASSERT_EQ(bdptr->_bingding_key,"news.music#");
}

TEST(bingding_test,remove_test)
{
    // bdmep->unbind("exchange1","queue2");
    // bitmq::QueueBingdingMap qbmap=bdmep->getExchangeBingdings("exchange1");
    // ASSERT_EQ(qbmap.find("queue2"),qbmap.end());

    // bdmep->removeExchangeBingdings("exchange1");
    // ASSERT_EQ(bdmep->exists("exchange1","queue3"),false);

    bdmep->removeQueueBingdings("queue3");
    ASSERT_EQ(bdmep->exists("exchange2","queue3"),false);
}
int main(int argc,char*argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new Binding_Test);
    RUN_ALL_TESTS();
    return 0;
}